package com.qs.rocketmq.common.base;

import cn.hutool.core.util.StrUtil;
import com.qs.rocketmq.common.util.ErrorMsgCacheUtil;
import com.qs.rocketmq.common.util.JsonResult;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BaseController {

    public JsonResult successed(Object obj) {
        return this.successed(obj, null);
    }

    public JsonResult successed(Object obj, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(true);
        jsonResult.setReturnObject(obj);
        jsonResult.setMessage(message);
        return jsonResult;
    }

    public JsonResult failed(long errCode) {
        return this.failed(errCode, null);
    }

    public JsonResult failed(long errCode, String errMsg) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);
        jsonResult.setCode(errCode);
        if (StrUtil.isBlank(errMsg)) {
            jsonResult.setMessage(ErrorMsgCacheUtil.getErrorMsg(errCode));
        } else {
            jsonResult.setMessage(errMsg);
        }
        return jsonResult;
    }
}
